﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace CP.Repositories.GPM
{
    public class FormAuthRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<User_Group> GetUser_Groups(string sql)
        {
            sql = "select * from User_Group where 1=1 " + sql + " order by group_id";
            return db.ExecuteObject<User_Group>(sql);
        }

        public IList<Webpart> GetAuthForms(int groupid)
        {
            string sql = string.Format(@"SELECT *
                      FROM webpart t1
                     WHERE EXISTS (
                               SELECT NULL
                                 FROM webpart_right t2
                                WHERE t1.webpart_id = t2.webpart_id AND GROUP_ID = {0}
                                      AND right_flag = 'R')", groupid);
            return db.ExecuteObject<Webpart>(sql);
        }

        public IList<Module> GetAuthModules(int groupid)
        {
            string sql = string.Format(@"SELECT t1.*
                          FROM module t1,
                               (SELECT DISTINCT module_id
                                  FROM webpart t1
                                 WHERE EXISTS
                                              (SELECT NULL
                                                 FROM webpart_right t2
                                                WHERE     t1.webpart_id = t2.webpart_id
                                                      AND GROUP_ID = {0}
                                                      AND right_flag = 'R')) t2
                         WHERE t1.module_id = t2.module_id", groupid);
            return db.ExecuteObject<Module>(sql);
        }

        public IList<Module> GetUnAuthModules(int groupid)
        {
            string sql = string.Format(@"SELECT t1.*
                          FROM module t1,
                               (SELECT DISTINCT module_id
                                  FROM webpart t1
                                 WHERE not EXISTS
                                              (SELECT NULL
                                                 FROM webpart_right t2
                                                WHERE     t1.webpart_id = t2.webpart_id
                                                      AND GROUP_ID = {0}
                                                      AND right_flag = 'R')) t2
                         WHERE t1.module_id = t2.module_id", groupid);
            return db.ExecuteObject<Module>(sql);
        }

        public IList<Webpart> GetUnAuthForms(int groupid)
        {
            string sql = string.Format(@"SELECT *
                      FROM webpart t1
                     WHERE not EXISTS (
                               SELECT NULL
                                 FROM webpart_right t2
                                WHERE t1.webpart_id = t2.webpart_id AND GROUP_ID = {0}
                                      AND right_flag = 'R')", groupid);
            return db.ExecuteObject<Webpart>(sql);
        }

        public int FormAuth(int groupid, IList<int> forms)
        {
            string errmsg = string.Empty;

            try
            {
                OracleParameter[] parameters ={
                new OracleParameter("p_groupid",OracleDbType.Int32),
                new OracleParameter("p_forms",OracleDbType.Varchar2,4000),
                new OracleParameter("p_errmsg",OracleDbType.Varchar2,200)
　　　　　　　　　　　　　};
                parameters[0].Direction = ParameterDirection.Input;
                parameters[1].Direction = ParameterDirection.Input;
                parameters[2].Direction = ParameterDirection.Output;

                parameters[0].Value = groupid;
                parameters[1].Value = string.Join(",", forms.ToArray());

                db.ExecuteNonQuery("gpm.proc_form_auth", parameters, CommandType.StoredProcedure);
                errmsg = parameters[2].Value.ToString();
                if (string.IsNullOrEmpty(errmsg))
                    return 1;
                throw new Exception(errmsg);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int FormUnAuth(int groupid, IList<int> forms)
        {
            string errmsg = string.Empty;

            try
            {
                OracleParameter[] parameters ={
                new OracleParameter("p_groupid",OracleDbType.Int32),
                new OracleParameter("p_forms",OracleDbType.Varchar2,4000),
                new OracleParameter("p_errmsg",OracleDbType.Varchar2,200)
　　　　　　　　　　　　　};
                parameters[0].Direction = ParameterDirection.Input;
                parameters[1].Direction = ParameterDirection.Input;
                parameters[2].Direction = ParameterDirection.Output;

                parameters[0].Value = groupid;
                parameters[1].Value = string.Join(",", forms.ToArray());

                db.ExecuteNonQuery("gpm.proc_form_unauth", parameters, CommandType.StoredProcedure);
                errmsg = parameters[2].Value.ToString();
                if (string.IsNullOrEmpty(errmsg))
                    return 1;
                throw new Exception(errmsg);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

    }
}
